// 获取URL中的参数部分
 const urlParams = new URLSearchParams(window.location.search);
 // 获取名为info的参数值
 const info = urlParams.get('info');
 const markdownPath = 'resources/'+info + '.md';
 // var a = info.md
 console.log(markdownPath)
//  if (info) {
//  document.write(`接收到的信息: ${info}`);
//  } else {
//  document.write('没有接收到信息');
//  }  
 
// 加载Markdown内容
fetch(markdownPath)
  .then(response => response.text())
  .then(text => {
    const contentDiv = document.getElementById('content');
    contentDiv.innerHTML = marked.parse(text);
    // 生成大纲
    const headings = [...contentDiv.querySelectorAll('h1, h2, h3')];
    const outlineDiv = document.getElementById('outline');

    headings.forEach(heading => {
      if (!heading.id) {
        heading.id = heading.textContent.toLowerCase().replace(/\s+/g, '-');
      }
      const anchor = document.createElement('a');
      anchor.href = `#${heading.id}`;
      anchor.textContent = heading.textContent;
      anchor.style.display = 'block';
      anchor.style.marginLeft = `${(parseInt(heading.tagName[1]) - 1) * 20}px`;
      outlineDiv.appendChild(anchor);
    });

    // 滚动同步
    contentDiv.addEventListener('scroll', () => {
      outlineDiv.scrollTop = contentDiv.scrollTop;
    });
  })
  .catch(err => console.error('加载Markdown文件失败:', err));

// 键盘滚动控制
document.addEventListener('keydown', (e) => {
  const content = document.getElementById('content');
  const scrollAmount = 100;
  
  switch(e.key) {
    case 'ArrowUp':
      content.scrollBy(0, -scrollAmount);
      break;
    case 'ArrowDown':
      content.scrollBy(0, scrollAmount);
      break;
  }
});

// 添加复制代码功能
function initCopyButtons() {
    document.querySelectorAll('.copy-btn').forEach(btn => {
        btn.addEventListener('click', () => {
            const codeBlock = btn.closest('pre').querySelector('code');
            navigator.clipboard.writeText(codeBlock.innerText)
                .then(() => {
                    const originalHTML = btn.innerHTML;
                    btn.innerHTML = '✓ 已复制';
                    btn.style.opacity = '1';
                    setTimeout(() => {
                        btn.innerHTML = originalHTML;
                        btn.style.animation = 'fadeOut 5s';
                    }, 5000);
                });
        });
    });
}

// 初始化时执行
window.addEventListener('DOMContentLoaded', initCopyButtons);